
<template>
  <page-header-wrapper :title="false">
    <a-card :bordered="false">
      <div class="table-page-search-wrapper">
        <a-form layout="inline">
          <a-row :gutter="24">
            <a-col :span="4">
              <a-form-item label="账号">
                <a-input v-model="queryParam.username" placeholder="请输入" />
              </a-form-item>
            </a-col>
            <!-- <a-col :span="4">
              <a-form-item label="是否会员">
                <a-select v-model="queryParam.vipState" placeholder="请选择" default-value="">
                  <a-select-option value="0">否</a-select-option>
                  <a-select-option value="1">是</a-select-option>
                </a-select>
              </a-form-item>
            </a-col> -->
            <a-col :span="4">
              <a-form-item label="代理商">
                <a-select v-model="queryParam.agentId" placeholder="请选择" default-value="">
                  <a-select-option :value="item.id" v-for="item in agentList" :key="item.id">{{ item.agentName }}</a-select-option>
                </a-select>
              </a-form-item>
            </a-col>
            <a-col :span="4">
              <a-form-item label="推荐人">
                <a-select
                  show-search
                  placeholder="请选择"
                  style="width: 100%"
                  :options="optionsSearch"
                  :filter-option="false"
                  @search="filterOptionSearch"
                  v-model="queryParam.topUserId"
                ></a-select>
              </a-form-item>
            </a-col>
            <a-col :span="6" style="text-align: left;">
              <span class="table-page-search-submitButtons">
                <!-- <a-button type="primary" icon="plus" @click="handleAdd">新建</a-button> -->
                <a-button type="primary" style="margin-right: 8px" @click="handleSearch">查询</a-button>
                <a-button style="margin-right: 8px" @click="handleReset">重置</a-button>
                <!-- <a-button type="primary" @click="handleExport">导出</a-button> -->
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <s-table
        ref="table"
        size="default"
        :scroll="{x: true}"
        :columns="columns"
        :data="loadData"
        :pageNum="pageNum"
        :pageSize="pageSize"
        rowKey="id">
        <div slot="action" slot-scope="text, record">
          <a type="primary"  @click="handlePassword(record)">重置密码</a>
          <a type="primary"  style="margin-left:10px;" @click="handleRole(record)">编辑</a>
        </div>
        <div slot="avatar" slot-scope="text, record">
          <img :src="text" height="70px" style="min-width: 70px;" preview="1"/>
        </div>
      </s-table>
    </a-card>
    <user-role ref="userRole" @ok="handleRoleOk"/>
    <password-form ref="password" @ok="handlePasswordOk"/>
  </page-header-wrapper>
</template>

<script>
import { STable } from '@/components'
import { list, userPassword } from '@/api/base'
import { userVipExport } from '@/api/export'
import UserRole from './modules/setrole-form'
import PasswordForm from './modules/password-form'
import { allotRole } from '@/api/role'
import { listByPage } from '@/api/agent'
import { listUserForSearch } from '@/api/user'
// import CreateForm from './modules/supplier-form'
export default {
  name: 'TableList',
  components: {
    STable,
    UserRole,
    PasswordForm
  },
  // status 1-激活 2-冻结 3-删除
  data () {
    return {
      queryParam: {},
      pageNum: 1,
      pageSize: 10,
      visible: false,
      mdl: null,
      confirmLoading: false,
      columns: [
        {
          title: '姓名',
          align: 'center',
          dataIndex: 'realName'
        },
        {
          title: '头像',
          align: 'center',
          dataIndex: 'avatar',
          scopedSlots: { customRender: 'avatar' }
        },
        {
          title: '手机号',
          align: 'center',
          dataIndex: 'phone'
        },
        {
          title: '推荐人',
          align: 'center',
          dataIndex: 'topUserName'
        },
        {
          title: '推荐总人数',
          align: 'center',
          dataIndex: 'teamCount'
        },
        {
          title: '累计消费总额',
          align: 'center',
          dataIndex: 'sumPrice'
        },
        {
          title: '累计退款总额',
          align: 'center',
          dataIndex: 'refundPrice'
        },
        {
          title: '实际消费总额',
          align: 'center',
          dataIndex: 'realPrice'
        },
        {
          title: '注册时间',
          align: 'center',
          dataIndex: 'createTime'
        },
        {
          title: '可用金币数',
          align: 'center',
          dataIndex: 'coin'
        },
        {
          title: '角色',
          align: 'center',
          dataIndex: 'roleNames',
          customRender: (val) => {
            return val && val.join(',')
          }
        },
        {
          title: '所属代理商',
          width: 200,
          align: 'center',
          dataIndex: 'agentName'
        },
        {
          title: '操作',
          width: 180,
          align: 'center',
          fixed: 'right',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' }
        }
      ],
      optionsSearch: [],
      agentList: []
    }
  },
  created () {
    this.listAgents()
    this.filterOptionSearch('')
  },
  methods: {
    filterOptionSearch (input) {
      listUserForSearch({query: input}).then(res => {
          this.optionsSearch = res.data.map(item => {
              return {
                  ...item,
                  value: item.id,
                  label: item.realName + '（' + item.mobile + ')'
              }
          })
      }).catch((err) => {})
    },
    listAgents () {
      listByPage({page: 1,limit: 1000}).then(res => {
        this.agentList = res.data.records
      }).catch(() => {})
    },
    handleSearch (e) {
      this.$refs.table.refresh()
    },
    handlePassword (record) {
      this.$refs.password.visible = true
      this.$refs.password.mdl = { ...record }
    },
    handleRole (record) {
      this.$refs.userRole.visible = true
      this.$refs.userRole.mdl = {
        realName: record.realName,
        id: record.id,
        op: record.op,
        topUserId: record.topUserId,
        roleId: record.roleIds && record.roleIds.length && record.roleIds[0] || ''
        // ...record
      }
    },
    handleRoleOk () {
      const form = this.$refs.userRole.form
      form.validateFields((errors, values) => {
        if (!errors) {
          values.userId = values.id
          delete values.id
          allotRole(values).then(res => {
            this.$refs.userRole.visible = false
            this.$message.success('设置成功')
            // 新增/修改 成功时，重载列表
            this.$refs.table.refresh()
            form.resetFields()
          }, () => {
            this.$refs.userRole.visible = false
          })
        }
      })
    },
    handlePasswordOk () {
      const form = this.$refs.password.form
      form.validateFields((errors, values) => {
        if (!errors) {
          userPassword(values).then(res => {
            this.$refs.password.visible = false
            this.$message.success('设置成功')
            // 新增/修改 成功时，重载列表
            this.$refs.table.refresh()
            form.resetFields()
          })
        }
      })
    },
    handleCancel () {
      this.visible = false
      this.$refs.createModal.fileList = []
      const form = this.$refs.createModal.form
      form.resetFields() // 清理表单数据（可不做）
    },
    // handleExport() {
    //   if (!this.$refs.table.localDataSource.length) {
    //     this.$message.warning('列表为空，不能导出')
    //     return
    //   }
    //   userVipExport(this.queryParam)
    // },
    handleReset () {
      this.queryParam = {}
      this.$refs.table.refresh()
    },
    loadData (params) {
      return list(Object.assign(params, this.queryParam)).then(res => {
        return res.data
      })
    },
  }
}
</script>
